<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>UMD Room Finder</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <script type="text/javascript">

        function getPic(){
            
            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                document.getElementById("picDiv").innerHTML=xmlhttp.responseText;
                }
              }
            xmlhttp.open("GET","updatePicture.php?Room="+picture,true);
            xmlhttp.send();
        };
        function formSubmit(){
        	document.getElementById("map_formsubmit").submit();
        	}

      </script>
    </head>
    <body>
        
  <style type="text/css">
      body
      {
      background-color:#e5f5ff;
      background-image:url(http://img.photobucket.com/albums/v627/lovkurama/Screenshot2011-10-03at53227PM.png);
      background-repeat:repeat-x;
      font-size: 1em;
      }
      div.banner
      {
      text-align:center;
      }
      
   #inhalt{
	text-align: center;
	vertical-align: middle;
	margin: 0px auto;
	padding: 0px;
	width: 40%;
	background-color: #e5f5ff;
	
	}
   </style>
        
        
        <div class="banner">
        
        <img src="http://img.photobucket.com/albums/v627/lovkurama/Screenshot2011-10-11at103904AM.png" height=30>
			</div>
			<div id="inhalt">
        <?php
                //connect to database
                $link = mysql_connect('localhost', 'pott0180', 'ckersixt');
                if(!$link) {
                        die('Could not connect: ' . mysql_error());
                }

                //select database
                $db_selected = mysql_select_db('soloono_roomFinder', $link);
                if (!$db_selected) {
                        die('Cant use db soloono_roomFinder: ' . mysql_error());
                }

        //declare variables
        $room_from=$_POST['Room_from'];
        $building_from=$_POST['Building_from'];
		
        $room_to=$_POST['Room_to'];
        $building_to=$_POST['Building_to'];
		
        //gets the building picture url
        $building_to_query = sprintf("Select Pictures.url from Pictures
                                    Inner Join Building on Building.picture_id = Pictures.id
                                    where Building.name = '$building_to'");
        $building_results = mysql_query($building_to_query);
        $building_array = mysql_fetch_array($building_results) or die(mysql_error());
        $building_to_url = $building_array[0];
		
        // gets the room picture url
        $room_picture_query = sprintf("Select Pictures.url from Pictures
                                    Inner Join Room on Room.pic_id = Pictures.id
                                    where Room.room_number = '$room_to'");
        $picture_results = mysql_query($room_picture_query);
        $picture_array = mysql_fetch_array($picture_results);
        $picture_url = $picture_array[0];

		  //get floor number for destination room
        $floor_query = sprintf("SELECT floor FROM Room WHERE room_number='$room_to'");
        $floor_results = mysql_query($floor_query);
        $floor_array = mysql_fetch_array($floor_results) or die(mysql_error());
        $floor_number = $floor_array[0];
        
        $building_coords_query = sprintf("SELECT building_coords FROM Building Where Building.name = '$building_to'");
        $building_coords_results = mysql_query($building_coords_query);
        $building_coords_array = mysql_fetch_array($building_coords_results) or die(mysql_error());
        $building_coords = $building_coords_array[0];

        echo '<h1>Room Finder</h1>';
        echo '<h3>Your Map</h3>';

		  echo 'The room <b>'.$room_to. '</b> in <b>'.$building_to.'</b> is on floor <b>'.$floor_number.'</b>.<br><br>';
        echo "<div>Your location:    <b>".$building_from." ".$room_from."</b></div>";
        
        echo '<form action="floorplan.php" method="post">';
        echo "<input type='hidden' value='".$picture_array["url"]."' name='Floorplan' />";
        echo "Your destination: <b> ".$building_to." ".$room_to." </b> <input type='submit' value='Click here for your room'/><br>";
        echo '</form>';
        echo '<hr>';
        echo '<br />';

        //Image urls stored in the database.
        //Respective images displayed
        echo '<div id="picDiv">';
        
	
		  if($building_from == $building_to){
		  	echo 'Your current location and destination are in the same building<br>';
		  	if($room_from == $room_to){
		  	echo 'as are your current and destination rooms. Please Start Over.<br>';
		  		}
		   echo '<br>';
		  	}
		  else{
        echo '<img id="buildingPicture" src="'.$building_array["url"].'" alt="building map" /><br><br>';  
		  echo '<area shape="rect" coords="'.$building_coords.'" onclick="formSubmit()" alt="room map"/>';
		  //if time allows, add an image map and figure out why js isn't allowing for variables to be passed to next page
		  //with a form and hidden variables
		  }

        echo '</div><hr>';
        
        echo 'Location > Destination > <b>Building Map</b> > Floor Map<br><br>';
        
        echo '<a href="index.php"><img SRC="http://i1120.photobucket.com/albums/l499/mill4655/roomFinder/StartOver.jpg" width=90 height=28  ALT="Start Over"></a><br>';
        
        echo "<br />For a full campus map, click <a href='http://www.d.umn.edu/maps/big_UMD_map.gif'>here</a><br />";
		  echo "For help, credits, more info, click <a href='about.html'>here</a><br>";
        
        ?>
</div>
    </body>
</html>
